<template>
  <div
    class="chart-original"
    :class="className"
    :style="{ height: height, width: width }"
  />
</template>

<script>
import echarts from "echarts";
import resize from "./mixins/resize";

export default {
  mixins: [resize],
  props: {
    name: {
      type: String,
      required: true,
    },
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "350px",
    },
    autoResize: {
      type: Boolean,
      default: true,
    },
    chartData: {
      type: Object,
      required: true,
    },
    xAxisName: {
      type: String,
    },
    legendName: {
      type: String,
      default: '版本'
    }
  },
  data() {
    return {
      chart: null,
    };
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {    
        this.setOptions(val);
      },
    },
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart();
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el);
      this.setOptions(this.chartData);
    },
    setOptions(expectedData) {
      this.chart.clear();
      let seriesArr = [];
      let legendDataArr = [];
      expectedData.yAxisData.forEach((item, index) => {
        let obj = {
          // smooth: true, // 曲线
          symbolSize: 8,
          symbol: 'circle',
          type: 'line',
          animationDuration: 2800,
          animationEasing: 'cubicInOut',
          itemStyle: {
            normal: {
              // color: '#40a9ff',
              lineStyle: {
                // color: '#40a9ff',
                width: 2
              }
            }
          },
        }
        obj['name'] = this.legendName + expectedData['legendData'][index];
        obj['type'] = 'line';
        obj['data'] = item;
        if (index == 0) {
          obj['itemStyle'] = {
            normal: {
              color: '#40a9ff',
              lineStyle: {
                color: '#40a9ff',
                width: 2
              }
            }
          }
        }
        seriesArr.push(obj)
      })
      expectedData.legendData.forEach(item => {
        let obj = {};
        obj['name'] = this.legendName + item;
        obj['icon'] = 'rect';
        legendDataArr.push(obj);
      })
      this.chart.setOption({
        tooltip: {
          trigger: 'axis',
          xAxisPointer: {
            type: 'cross'
          },
          padding: [5, 10]
        },
        legend: {
          right: 30,
          data: legendDataArr, 
        },
        grid: {
          left: 40,
          right: 50,
          bottom: 35,
          top: 25,
          containLabel: true
        },
        toolbox: {
          feature: {
            saveAsImage: {},
          },
        },
        toolbox: {
          show: false
        },
        xAxis: {
          type: "category",
          data: expectedData.xAxisData,
          boundaryGap: false,
          axisTick: {
            show: true
          },
          name: this.xAxisName || "",
          nameLocation: "middle",
          nameGap: 25,
          nameRotate: 0,
        },
        yAxis: {
          type: "value",
          name: this.name,
          nameLocation: "end",
          nameGap: 10,
          nameRotate: 0,
          minInterval: 1,
          axisTick: {
            show: true
          },
          splitLine: {
            show: true,
            lineStyle: {
              type: 'dashed'
            }
          }
        },
        series: seriesArr,
      });
    },
  },
};

</script>

<style lang="scss" scoped>
.chart-original {
  margin: 0 16px;
}
</style>